package dapps.service.proj;

import java.util.List;
import java.util.Map;

import org.hibernate.criterion.Criterion;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Property;
import org.hibernate.criterion.Restrictions;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springside.modules.orm.hibernate.DefaultEntityManager;

import dapps.entity.proj.Plan;



//Spring Service Bean的标识.
@Service
//默认将类中的所有函数纳入事务管理.
@Transactional
public class PlanManager extends DefaultEntityManager<Plan, Long> {

	@Override
	public void delete(Long id) {
		//为演示异常处理及操作员行为日志而抛出的异常.
		entityDao.delete(id);
		
	}
	@Override
	public void save(Plan plan) {
		//为演示异常处理及操作员行为日志而抛出的异常.
		entityDao.save(plan);
		
	}
	@Transactional(readOnly = true)
	public boolean notHave(String propertyName, Object value) {
		return (entityDao.findUniqueByProperty(propertyName, value)==null);
	}
	
	@Transactional(readOnly = true)
	public Plan getPlan(String propertyName, Object value) {
		return entityDao.findUniqueByProperty(propertyName, value);
	}
	@Transactional(readOnly = true)
	public Integer getMaxPlaned(Integer yeartime) {
		Criterion[] criterions = {
				Property.forName("yeartime").between(yeartime/100*100,(yeartime/100+1)*100-1)	
				,Restrictions.ge("status", 5)
				};
		String[] pn = {"max_yeartime"};
		Map<String,Object> staticsMap =entityDao.staticsByCriteria(pn,criterions,null);
		return staticsMap.get(pn[0])==null?0:(Integer)staticsMap.get(pn[0]);
	}
	public List<Plan> getAllPlansOrder(String ordername) {
		return entityDao.getAll(Order.desc(ordername));
	}
}
